!> author: 左志华
!> date: 2022-07-19
!>
!> 命令行界面：<br>
!> 1. 前处理：`to-sph pre --type:T 'lua'`；<br>
!> 2. 后处理：`to-sph post --type:T 'vtk'`
!> @note 命令行界面覆盖 TOML 文件中的配置，如果 TOML 文件和 CLI 中都没有配置，则使用默认配置（lua->h5part->?）。
module sph_command_line

    use, intrinsic :: iso_c_binding, only: newline => c_new_line
    use argparse, only: argparser
    use seakeeping_filesystem, only: is_exist, is_windows, windows_path
    implicit none
    private

    public :: get_cmd_settings, cmd_settings_t, &
              pre_settings_t, post_settings_t

    !> CLI 抽象类
    type, abstract :: cmd_settings_t
        character(:), allocatable :: working_dir    !! Working directory <br>
                                                    !! 工作目录
        character(:), allocatable :: type           !! Pre-processing type <br>
                                                    !! 前处理类型
        logical :: skip = .false.                   !! Skip pre-processing asking <br>
                                                    !! 跳过前处理询问
    end type cmd_settings_t

    !> 前处理类
    type, extends(cmd_settings_t) :: pre_settings_t
    end type pre_settings_t

    !> 后处理类
    type, extends(cmd_settings_t) :: post_settings_t
    end type post_settings_t

contains

    !> 获取命令行参数
    subroutine get_cmd_settings(cli, cmd_settings)
        type(argparser), intent(out) :: cli  !! 命令行解析器
        class(cmd_settings_t), allocatable, intent(out) :: cmd_settings     !! Command line settings <br>
                                                                            !! 命令行参数
        character(*), parameter :: version_text(*) = &
            &[character(55) :: &
            "Version    : 0.3.5, alpha (Built on "//__DATE__//')', &
            "Program    : starter(2D)", &
            "Description: pre-processing tool for ti-sph", &
            "Home Page  : https://gitee.com/fortran-sph/ti-sph", &
            "Author     : ZUO Zhihua (e-mail: zuo.zhihua@qq.com)", &
            "License    : BSD-3-Clause"]

        cli = argparser("Pre-processing tool for ti-sph.")
        call cli%set_program_name("starter")
        call cli%add_help_option()
        call cli%add_sc_option('-v', '--version', 'show version info', show_version_info)
        call cli%add_option_logical('', '--pre', 'use pre-processing')
        call cli%add_option_logical('', '--post', 'use post-processing')
        call cli%add_option_logical('', '--skip', 'skip asking')
        call cli%add_option_string('-C', '--directory', "working directory", '.')
        call cli%add_option_string('-t', '--type', 'processing file type', 'lua')
        call cli%parse()

        if (cli%get_option_logical('--pre')) then
            allocate (pre_settings_t :: cmd_settings)
            cmd_settings%working_dir = trim(cli%get_option_string('--directory'))
            cmd_settings%type = trim(cli%get_option_string('--type'))
            cmd_settings%skip = cli%get_option_logical('--skip')
        else
            error stop "only pre subcommand is supported"
        end if

        if (is_windows()) cmd_settings%working_dir = windows_path(cmd_settings%working_dir)

        if (.not. is_exist(cmd_settings%working_dir, dir=.true.)) then
            error stop "path does not exists, "//cmd_settings%working_dir
        end if

    contains

        !> 显示版本信息
        subroutine show_version_info()
            print '(*(a/))', version_text
        end subroutine show_version_info

    end subroutine get_cmd_settings

end module sph_command_line
